
import rcontrolutil
import rcontrol
import Tkinter


#instead of using the StringVar/IntVar , and setting precision that way,  or with Digits property,
#simply set the resolution property, and don't use any variable- just the get() and set() methods.
#~ --highlightcolor,highlightbackground,highlightthickness,s
#~ *tickinterval* - Display "tick" marks. Set to 0.1 to draw a mark after every 0.1. Default is 0, which means not to show ticks.
#tickinterval just displays text to the side, doesn't look very good, and only works for large intervals. If too small interval, doesn't show at all

class RSlider(rcontrol.RControl):
	"""
	Properties:
	
	*value* - Current value
	*range* - Tuple, of start to stop position. Example: (0,100) 
	
	*height* - Height, in pixels.
	*width* - Width, in pixels.
	
	*onchanged* - Function (taking one argument) that is called upon value changed. None represents nothing bound.
	*resolution* - Set to smallest increment that can be set. For example, set to 0.1 to allow 0.1 resolution. Set to -1 to disable rounding.
	
	*enabled* - True or False. If False, the slider is disabled.
	*label* - Show text near the control.
	*showvalue* - Shows current value, defaults to True.
	*sliderlength* - Length of the slider. Defaults to 30 pixels.
	
	*color* - Foreground color of the control, to use for text. RGB tuple.
	*bgcolor* - Background color of the control. RGB tuple, such as (255,0,0) for pure red.
	*font* - Set font of text. Standard tkinter font format, such as ('Times New Roman', 12, 'normal')
	*borderwidth* - Integer, defaults to 1 or 2.
	*borderstyle* - Border style. One of 'sunken','raised','groove','ridge','flat'. Default is 'raised'. Set to 'sunken' and it will appear pressed.
	
	"""

	_basecontrol = None
	_bVertical = None
	def __init__(self, range=(0,100), bVertical=True, parent=None, **options):
		self._newWidgetManual(parent, Tkinter.Scale)
		
		#orientation can only be set during creation.
		if bVertical: self._basecontrol.config(orient=Tkinter.VERTICAL)
		else: self._basecontrol.config(orient=Tkinter.HORIZONTAL)
		
		self._bVertical = bVertical
		self.range = range
		self.set_many(**options)
	
	
	def property_value_get(self):
		return self._basecontrol.get()
	def property_value_set(self, value):
		self._basecontrol.set(value)
	
	#correct range for Vertical, so that up is up
	def property_range_get(self):
		if not self._bVertical: return (self._basecontrol['from'], self._basecontrol['to'])
		else: return (self._basecontrol['to'], self._basecontrol['from'])
	def property_range_set(self, value):
		if not self._bVertical: (self._basecontrol['from'], self._basecontrol['to']) = value
		else: (self._basecontrol['to'], self._basecontrol['from']) = value
	
	#Tk reverses height/width depending on orientation, we correct for this
	def property_height_get(self):
		if self._bVertical: return self._basecontrol['length']
		else: return self._basecontrol['width']
	def property_height_set(self, value):
		if self._bVertical: self._basecontrol['length'] = value
		else: self._basecontrol['width']=value
	def property_width_get(self):
		if self._bVertical: return self._basecontrol['width']
		else: return self._basecontrol['length']
	def property_width_set(self, value):
		if self._bVertical: self._basecontrol['width'] = value
		else: self._basecontrol['length']=value
	
	#In tkinter, if you set the 'command' to None, it doesn't remove the binding. this fixes that.
	def property_onchanged_get(self): #convert '' or 0 to None
		if not self._basecontrol['command']: return None
		else: return self._basecontrol['command']
	def property_onchanged_set(self, value):
		if value: self._basecontrol['command'] = value
		else: self._basecontrol['command'] = ''
	
	
		
	def property_enabled_get(self):
		if self._basecontrol['state']==Tkinter.DISABLED: return False
		else: return True
	def property_enabled_set(self, value):
		if value: self._basecontrol['state'] = Tkinter.NORMAL
		else: self._basecontrol['state'] = Tkinter.DISABLED
			
	


	_properties = {

		'value': (property_value_get, property_value_set),
		'range': (property_range_get, property_range_set),
		'height': (property_height_get, property_height_set),
		'width': (property_width_get, property_width_set),
		'onchanged': (property_onchanged_get, property_onchanged_set),
		'resolution': 'resolution',
		#~ 'tickinterval': 'tickinterval',
		'enabled': (property_enabled_get, property_enabled_set),
		'label': 'label',
		'showvalue': 'showvalue',
		'sliderlength': 'sliderlength',
		
		'bgcolor': ('background',rcontrolutil.mapColor),
		'color': ('foreground',rcontrolutil.mapColor),
		'borderstyle': ('relief',rcontrolutil.mapBorderstyle),
		'font': 'font',	
		'borderwidth': 'borderwidth',
		}




